import pygame
from pygame.locals import *
import time
import random
import sys
#下面是自己编写的模块
import Arrow
import Bow
import Goku
import GameSetting

#-------------------------------------------
#定义几个变量
#-------------------------------------------
arrowList1=[]
arrowList2=[]
arrowList3=[]
arrowList4=[]  #存放箭头的列表
cont=0         #计数器
setting=GameSetting.GameSetting()
pause=False #控制游戏的暂停
#------------------------------------------
pygame.init()
pygame.mixer.init() #初始化混音器
pygame.time.delay(1000) #等待1秒让mixer完成初始化
sound=pygame.mixer.Sound('move.wav')
screen=pygame.display.set_mode((setting.screen_width,setting.screen_height),0,32)
move_sound=pygame.mixer.Sound('move.wav')
pygame.mixer.music.load('bgm.wav') #加载背景音乐
pygame.mixer.music.play(-1,0.0)  #循环从头播放bgm
pygame.display.set_caption(setting.cap)
musicPlaying=True
#创建人物和子弹，炮台
goku=Goku.Goku(screen)
bow_topleft=Bow.Bow(screen,0,0)
bow_topleft.image=pygame.transform.flip(bow_topleft.image,True,True) #翻转图片
bow_topright=Bow.Bow(screen,800,0)
bow_topright.image=pygame.transform.flip(bow_topleft.image,True,False)
bow_bottomleft=Bow.Bow(screen,0,600)
bow_bottomleft.image=pygame.transform.flip(bow_topleft.image,False,True)
bow_bottomright=Bow.Bow(screen,800,600)


alpha=random.randint(-80,-10) #随机生成一个合适的出射角度
tmpPic=Arrow.Arrow(screen,bow_topleft,alpha) #先生成一支箭
tmpPic.image=pygame.transform.rotate(tmpPic.image,alpha) #翻转到合适的角度
arrowList1.append(tmpPic) #左上角射出的箭

alpha=random.randint(190,260)
tmpPic=Arrow.Arrow(screen,bow_topright,alpha) #生成一支箭
tmpPic.image=pygame.transform.rotate(tmpPic.image,alpha) #旋转到合适的角度
arrowList2.append(tmpPic)

alpha=random.randint(10,80)
tmpPic=Arrow.Arrow(screen,bow_bottomleft,alpha)
tmpPic.image=pygame.transform.rotate(tmpPic.image,alpha) #旋转到合适的角度
arrowList3.append(tmpPic)

alpha=random.randint(100,170)
tmpPic=Arrow.Arrow(screen,bow_bottomright,alpha)
tmpPic.image=pygame.transform.rotate(tmpPic.image,alpha) #旋转到合适的角度
arrowList4.append(tmpPic)

while True:
    
    cont+=1
    if cont>20:
        
        alpha=random.randint(-80,-10) #随机生成一个合适的出射角度
        tmpPic=Arrow.Arrow(screen,bow_topleft,alpha) #先生成一支箭
        tmpPic.image=pygame.transform.rotate(tmpPic.image,alpha) #转到合适的角度
        arrowList1.append(tmpPic) #左上角射出的箭
        
        alpha=random.randint(190,260)
        tmpPic=Arrow.Arrow(screen,bow_topright,alpha) #生成一支箭
        tmpPic.image=pygame.transform.rotate(tmpPic.image,alpha) #旋转到合适的角度
        arrowList2.append(tmpPic)

        alpha=random.randint(10,80)
        tmpPic=Arrow.Arrow(screen,bow_bottomleft,alpha)
        tmpPic.image=pygame.transform.rotate(tmpPic.image,alpha) #旋转到合适的角度
        arrowList3.append(tmpPic)

        alpha=random.randint(100,170)
        tmpPic=Arrow.Arrow(screen,bow_bottomright,alpha)
        tmpPic.image=pygame.transform.rotate(tmpPic.image,alpha) #旋转到合适的角度
        arrowList4.append(tmpPic)
        
        cont=0
    time.sleep(0.05)
    
    for j in arrowList1:
        
        j.fly()
        if j.IsOut()==True:
             arrowList1.remove(j)
    for j in arrowList2:
        
        j.fly()
        if j.IsOut()==True:
             arrowList2.remove(j) 
    for j in arrowList3:
        
        j.fly()        
        if j.IsOut()==True:
            arrowList3.remove(j)
    for j in arrowList4:
       
        j.fly()
        if j.IsOut()==True:
            arrowList4.remove(j) 

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type==KEYDOWN:
            if event.key==K_ESCAPE:
                pygame.quit()
                sys.exit()
            elif event.key==K_p:  #暂停功能
                pause=True
                while pause==True:
                    for event in pygame.event.get():
                        if event.type==KEYDOWN:
                            if event.key==K_p:
                                pause=False
                            if event.key==K_ESCAPE:
                                pygame.quit()
                                sys.exit()

            elif event.key==K_RIGHT:
                sound.play()
                goku.move(150,0)
            elif event.key==K_LEFT:
                sound.play()
                goku.move(-150,0)
            elif event.key==K_UP:
                sound.play()
                goku.move(0,-120)
            elif event.key==K_DOWN:
                sound.play()
                goku.move(0,120)
           
    screen.fill((setting.bg_color))
   
    for i in arrowList1:
        screen.blit(i.image,i.rect)
    for i in arrowList2:
        screen.blit(i.image,i.rect)
    for i in arrowList3:
        screen.blit(i.image,i.rect)
    for i in arrowList4:
        screen.blit(i.image,i.rect)
        
    goku.draw()   
    screen.blit(bow_topleft.image,bow_topleft.rect)
    screen.blit(bow_topright.image,bow_topright.rect)
    screen.blit(bow_bottomleft.image,bow_bottomleft.rect)
    screen.blit(bow_bottomright.image,bow_bottomright.rect)
    pygame.display.update()











